<template>
  <div>
    <div style="margin: 10px 0">
      <el-input v-model="name" placeholder="请输入名称" style="width: 200px" suffix-icon="el-icon-search" clearable></el-input>
      <el-button type="primary" style="margin-left: 5px" @click="load">搜索</el-button>
      <el-button type="warning" style="margin-left: 5px" @click="reset">重置</el-button>
    </div>
    <div style="margin: 10px 0">
      <el-upload style="display: inline-block" :show-file-list="false"
                 :on-success="handleFileUploadSuccess"
                 action="http://localhost:9090/file/upload">
        <el-button type="primary">上传文件<i class="el-icon-top"></i></el-button>
      </el-upload>
      <el-popconfirm
          class="ml-5 mr-5"
          confirm-button-text='我已经下定了决心'
          cancel-button-text='不，不要。不要啊'
          icon="el-icon-info"
          icon-color="red"
          title="这可是一堆数据哥们，好好思考一下？"
          @confirm="delBatch"
      >
        <el-button type="danger" slot="reference">批量删除 <i class="el-icon-remove-outline"></i></el-button>
      </el-popconfirm>
    </div>

    <el-table :data="tableData" border stripe @selection-change="handleSelectionChange"
              :header-cell-style="{background:'#eef1f6',color:'#606266'}">
      <el-table-column
          type="selection"
          width="55">
      </el-table-column>
      <el-table-column label="ID" prop="id" width="80"></el-table-column>
      <el-table-column label="文件名称" prop="name"></el-table-column>
      <el-table-column label="文件类型" prop="type"></el-table-column>
      <el-table-column label="文件大小(kb)" prop="size"></el-table-column>

      <el-table-column label="下载链接">
        <template #default="scope">
          <el-button type="primary" @click="download(scope.row.url)">下载</el-button>
        </template>
      </el-table-column>

      <el-table-column label="启用">
        <template #default="scope">
          <el-switch v-model="scope.row.enable" active-color="#13ce66" inactive-color="#ccc"
                     @change="changeEnable(scope.row)"></el-switch>
        </template>
      </el-table-column>

      <el-table-column label="操作" prop="handle" width="200">
        <template #default="scope">
          <el-popconfirm
              class="ml-5"
              confirm-button-text='确认删除'
              cancel-button-text='我再想想'
              icon="el-icon-info"
              icon-color="red"
              title="Are you Sure？"
              @confirm="handDelete(scope.row.id)"
          >
            <el-button type="danger" slot="reference">删除 <i class="el-icon-delete"></i>
            </el-button>
          </el-popconfirm>
        </template>
      </el-table-column>

    </el-table>
    <div class="demo-pagination-block" style="padding: 10px 0">
      <el-pagination
          :current-page="currentPage"
          :page-size="pageSize"
          :page-sizes="[5, 10, 15, 20]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script>
export default {
  name: "File",
  data() {
    return {
      tableData: [],
      name: '',
      multipleSelection: [],
      currentPage: 1,
      pageSize: 5,
      total: 0,
      form: {},
      dialogFormVisible: false,
    }
  },
  created() {
    this.load();
  },
  methods: {
    load() {
      this.request.get("/file/page", {
        params: {
          pageNum: this.currentPage,
          pageSize: this.pageSize,
          name: this.name,
        }
      }).then(res => {
        this.tableData = res.data.records
        this.total = res.data.total
      })
    },
    reset() {
      this.name = ''
      this.load()
    },
    changeEnable(row) {
      this.request.post("/file/update",row).then((res) => {
        console.log(res)
        if(res.code === '200'){
          this.$message.success("操作成功")
        }
      })
    },
    handDelete(id) {
      this.request.delete("/file/" + id).then((res) => {
        if (res.code === '200') {
          this.$message.success("删除成功")
          this.load()
        } else {
          this.$message.error("删除失败")
        }
      })
    },
    handleSelectionChange(val) {
      console.log(val); //val是一个对象
      this.multipleSelection = val
    },
    delBatch() {
      let ids = this.multipleSelection.map(value => value.id) // [{},{}] => [1,2...]
      this.request.post("/file/del/batch", ids).then((res) => {
        if (res.code === '200') {
          this.$message.success("批量删除成功")
          this.load()
        } else {
          this.$message.error("删除失败")
        }
      })
    },
    handleSizeChange(pageSize) {
      console.log(pageSize);
      this.pageSize = pageSize
      this.load()
    },
    handleCurrentChange(pageNum) {
      console.log(pageNum);
      this.currentPage = pageNum
      this.load()
    },
    handleFileUploadSuccess(res) {
      console.log(res)
      this.load()
    },
    download(url) {
      window.open(url)
      console.log('hello',url);
    },

  }
}
</script>

<style scoped>

</style>